// pages/mine/anchorWelfare/upgrade/upgrade.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: app.register_info.register_role,

    flag_update: true,
  },
  // 去等级规则页
  go_rule() {
    wx.navigateTo({
      url: '../rule/rule',
    })
  },
  // 升级按钮
  click_update() {
    if (this.data.upgrade.is_accord_rule == "1") {
      this.update();
    } else {
      wx.navigateTo({
        url: '../../invite/index'
      })
    }
  },
  //注册信息
  getInfo() {
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      responseType: 'text',
      success: (e) => {
        console.log(e)
        let avatar_url = e.data.data.register_info.avatar_url;
        this.setData({
          avatar_url: avatar_url,
        })
      },
    })
  },
  //升级
  update() {
    wx.request({
      url: app.globalData.url + 'api/broker/hand/upgrade',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log("ok");
        this.setData({
          flag_update: false,

        })
        this.getRule();

      },
    })
  },
  getRule() {
    wx.request({
      url: app.globalData.url + 'api/broker/lift/rule',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let welfare = e.data.data.welfare,
          rule = e.data.data.rule,
          upgrade = e.data.data,

          // register_anchor = 5,
          // register_resume = 2,

          // level = 2,

          register_anchor = upgrade.register_anchor ? parseInt(upgrade.register_anchor) : 0,
          register_resume = upgrade.register_resume ? parseInt(upgrade.register_resume) : 0,


          //注册数节点
          register_one = 0,
          register_two = parseInt(rule[0].invite_register_num),
          register_thr = parseInt(rule[1].invite_register_num),
          register_fou = parseInt(rule[2].invite_register_num),

          //简历数节点
          resume_one = 0,
          resume_two = parseInt(rule[0].invite_resume_num),
          resume_thr = parseInt(rule[1].invite_resume_num),
          resume_fou = parseInt(rule[2].invite_resume_num);
        var level_show = e.data.data.level;
        if (level_show == 3) {
          var level = upgrade.level ? parseInt(upgrade.level) : 0;
          console.log('level1+' + level)
        } else {
          var level = upgrade.level ? parseInt(upgrade.level) + 1 : 0;
          console.log('level2+' + level)
        }
        //注册数
        if (register_two > register_anchor && register_anchor >= register_one) {
          // console.log(1)
          this.setData({
            flag_one: true,
          })
        } else if (register_thr > register_anchor && register_anchor >= register_two) {
          // console.log(2)
          this.setData({
            flag_two: true,
            extra_one: true,
          })
        } else if (register_fou > register_anchor && register_anchor >= register_thr) {
          // console.log(3)
          this.setData({
            flag_three: true,
            extra_two: true,
            extra_one: true,
          })
        } else if (register_two == register_anchor) {
          this.setData({
            extra_one: true,
          })
        } else if (register_thr == register_anchor) {
          this.setData({
            extra_one: true,
            extra_two: true,
          })
        } else if (register_fou == register_anchor) {
          this.setData({
            extra_one: true,
            extra_two: true,
            extra_three: true,
          })
        }

        //简历数
        if (resume_two > register_resume && register_resume >= resume_one) {
          // console.log(1)
          this.setData({
            flag_one_1: true,
          })
        } else if (resume_thr > register_resume && register_resume >= resume_two) {
          // console.log(2)
          this.setData({
            flag_two_1: true,
            extra_one_1: true,
          })
        } else if (resume_fou > register_resume && register_resume >= resume_thr) {
          // console.log(3)
          this.setData({
            flag_three_1: true,
            extra_two_1: true,
            extra_one_1: true,
          })
        } else if (resume_two == register_resume) {
          this.setData({
            extra_one_1: true,
          })
        } else if (resume_thr == register_resume) {
          this.setData({
            extra_one_1: true,
            extra_two_1: true,
          })
        } else if (resume_fou == register_resume) {
          this.setData({
            extra_one_1: true,
            extra_two_1: true,
            extra_three_1: true,
          })
        }
        switch (level) {
          case 0:
            var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxc.png',
              level_position = 'top:262rpx;right:480rpx';
            break;
          case 1:

            var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxz.png',
              level_position = 'top:164rpx;right:254rpx';
            break;
          case 2:

            var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxg.png',
              level_position = 'top:84rpx;right:68rpx';
            break;
          case 3:
            var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxg.png',
              level_position = 'top:84rpx;right:68rpx';
            break;
        }
        // switch (level) {
        //   case 0:
        //     var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssx.png',
        //       level_position = 'top:330rpx;left:28rpx';
        //     break;
        //   case 1:
        //     var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxc.png',
        //       level_position = 'top:262rpx;right:480rpx';
        //     break;
        //   case 2:
        //     var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxz.png',
        //       level_position = 'top:164rpx;right:254rpx';
        //     break;
        //   case 3:
        //     var level_img = 'https://www.zhibohome.net/images/mine/yjr_ssxg.png',
        //       level_position = 'top:84rpx;right:68rpx';
        //     break;
        // }
        this.setData({
          rule: rule,
          welfare: welfare,
          upgrade: upgrade,

          level: level,
          level_img: level_img,
          level_position: level_position,

          register_anchor: register_anchor,
          register_resume: register_resume,

          //注册数节点
          register_one: register_one,
          register_two: register_two,
          register_thr: register_thr,
          register_fou: register_fou,

          //简历数节点
          resume_one: resume_one,
          resume_two: resume_two,
          resume_thr: resume_thr,
          resume_fou: resume_fou,

        })
      },
    })
  },
  //模块框 Start
  modal_hide: function() {
    this.setData({
      flag_update: true,
    })
  },
  //模块框 End
  //获取 页面rpx比例
  getRpx() {
    wx.getSystemInfo({
      success: (e) => {
        const rpx = e.windowWidth / 375;
        this.setData({
          rpx: rpx,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRpx();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getRule();
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})